﻿using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using LitJson;
using System;

public class Controller : MonoBehaviour {

    public AirBoxModel leftBox;
    public AirBoxModel rightBox;
    List<int> results ;
	// Use this for initialization
	void Start () {
        StartCoroutine(ConnectSQL());
	}
	
	// Update is called once per frame
	void Update () {
	
	}
    /// <summary>
    /// 链接数据库
    /// </summary>
    /// <returns></returns>
    public IEnumerator ConnectSQL()
    {
        while (true)
        {
            yield return new WaitForSeconds(0.02f);

            WWWForm wwf = new WWWForm();
            wwf.AddField("fun", "select_baojinghistoy");
            WWW www = new WWW("http://183.63.55.212:6602/lg/soa", wwf);
            yield return www;
            if (www.isDone)
            {
                results = new List<int>();
                string a = www.text;
                //Debug.LogError(a + "123");
                JsonData jd = JsonMapper.ToObject(a);
                //Debug.Log("status = " + (string)jd["status"]);
                JsonData jdItems = jd["rs"];
                try
                {
                    int num_1 = (int)jdItems[0]["point_1"];
                    results.Add(num_1);
                    print(num_1);
                    int num_2 = (int)jdItems[0]["point_2"];
                    results.Add(num_2);
                    print(num_2);
                    SendMessageToObject(results);
                }
                catch (Exception)
                {
                    Debug.Log("连接获取数据失败");
                }
            }

        }
    }

    private void  SendMessageToObject(List<int> list)
    {
        int count = 0;
        for (int i = 0; i < list.Count; i++)
        {
            if(list[i] > 0)         
                count++;       
        }
        print(count);
        switch(count)
        {
            case 0:
                Camera.main.GetComponent<CameraModel>().InitCamera();
                if( Camera.main.GetComponent<CameraModel>().isRotate)
                    Camera.main.GetComponent<CameraModel>().isRotate = false;
                if (leftBox.isChanged)
                {
                    leftBox.isChanged = false;
                    SendMessageToText(leftBox.transform, false);                
                }
                leftBox.InitColor();
                if (rightBox.isChanged)
                {
                    rightBox.isChanged = false; 
                    SendMessageToText(rightBox.transform, false);
                }
                rightBox.InitColor();
                break;
            case 2:
                if (!Camera.main.GetComponent<CameraModel>().isRotate)
                    Camera.main.GetComponent<CameraModel>().isRotate = true;
                if (!leftBox.isChanged)
                {
                    leftBox.isChanged = true;
                    SendMessageToText(leftBox.transform, true);    
                }
                if (!rightBox.isChanged)
                {
                    rightBox.isChanged = true;
                    SendMessageToText(rightBox.transform, true);
                }
                break;
            default:
                SetOneChange(list);
                break;

        }
    }
    void  SendMessageToText(Transform parent, bool isWarming)
    {
        ShowRandom[] texts = parent.GetComponentsInChildren<ShowRandom>();
        for (int i = 0; i < texts.Length; i++)
        {
            if (texts[i].isWarming != isWarming)
                texts[i].isWarming = isWarming;
        }
    }
    /// <summary>
    /// 传送一个报警信息
    /// </summary>
    /// <param name="list"></param>
    private void SetOneChange(List<int> list)
    {
        for (int i = 0; i < list.Count; i++)
        {
            if (Camera.main.GetComponent<CameraModel>().isRotate)
                Camera.main.GetComponent<CameraModel>().isRotate = false;
            if (list[i] > 0)
            {
                switch(i)
                {
                    case 0:
                        if (!leftBox.isChanged)
                        {
                            leftBox.isChanged = true;
                            SendMessageToText(leftBox.transform, true);
                        }
                        Camera.main.GetComponent<CameraModel>().LookAtLeftBox();
                        break;
                    case 1:
                        if (!rightBox.isChanged)
                        {
                            rightBox.isChanged = true;
                            SendMessageToText(rightBox.transform, true);
                        }
                        Camera.main.GetComponent<CameraModel>().LookAtRightBox();
                        break;
                }
            }
        }
    }
}
